package com.example.final_project.Usr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.MainView;
import com.example.final_project.R;
import com.example.final_project.Usr.entry.RegisterInfo;

public class RegisterActivity extends AppCompatActivity {

    private EditText Account,Pwd,PwdCheck,Name;
    private CheckBox checkBox;
    private Button ok,cannel;
    private RadioButton Boy,Girl;
    private TextView login;
    public static final int SECCESS=0;
    public static final int FALSE=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //找到相关控件
        findById();
        //给相关按钮设置监听事件
        ButtonListener();

    }

    //根据id查找相关控件
    public void findById(){
        Account = findViewById(R.id.register_account);
        Pwd = findViewById(R.id.register_pwd);
        PwdCheck = findViewById(R.id.register_pwdCheck);
        checkBox = findViewById(R.id.register_check);
        ok = findViewById(R.id.register_ok);
        cannel = findViewById(R.id.register_cancel);
        Name = findViewById(R.id.register_name);
        Boy = findViewById(R.id.register_boy);
        Girl = findViewById(R.id.register_girl);
        login = findViewById(R.id.register_login);
    }

    public void ButtonListener(){

        //已有账号进行登录功能
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        //取消按钮
        cannel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, MainView.class);
                intent.setAction("backToUserFrag");
                startActivity(intent);
            }
        });
        //确定按钮
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入的账号密码
                String name = Name.getText().toString();
                String account = Account.getText().toString();
                String pwd = Pwd.getText().toString();
                String pwdCheck = PwdCheck.getText().toString();
                String sex = null;
                //判断性别
                if(Boy.isChecked()){
                    sex = "男";
                }else {
                    sex = "女";
                }

                //判断两次密码是否输入一致
                if(!pwd.equals(pwdCheck)){
                    Toast.makeText(RegisterActivity.this,"请确认两次密码是否一致",Toast.LENGTH_SHORT).show();
                }else{
                    //注册前判断是否勾选协议服务
                    if(checkBox.isChecked()){
                        //将数据传入到服务器中，完成注册
                        registerToNet(name,account,pwd,sex);
                    }else {
                        Toast.makeText(RegisterActivity.this,"请勾选《橙车行隐私政策》",Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SECCESS:
                    RegisterInfo registerInfo = (RegisterInfo) msg.obj;
                    //注册成功之后，将注册信息放入本地存储，实现自动登录
                    SharedPreferences sp = RegisterActivity.this.getSharedPreferences("userInto", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("name",registerInfo.getName());
                    editor.putInt("age",0);
                    editor.putString("sex",registerInfo.getSex());
                    editor.putString("phone","xxx");
                    editor.putString("address","xxx");
                    editor.commit();
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    //跳转到登录界面进行登录
                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent);
                    break;
                case FALSE:
                    Toast.makeText(RegisterActivity.this,"账号已被注册",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    public void registerToNet(String name,String account,String pwd,String sex){

        RegisterInfo registerInfo = new RegisterInfo(account, pwd, name, sex);

            OkHttps.async("http://10.150.11.9:8086/chengcar/register")
                    .bind(RegisterActivity.this)
                    .addUrlPara("account",account)
                    .addUrlPara("password",pwd)
                    .addUrlPara("name",name)
                    .addUrlPara("sex",sex)
                    .setOnResString(check ->{
                        Message message = Message.obtain();
                        if(check.equals("true")){
                            message.what = SECCESS;
                            message.obj = registerInfo;
                        }else{
                            message.what = FALSE;
                        }
                        handler.sendMessage(message);
                    })
                    .get();
    }

}